package ru.urfc.portal.dao.impl;

import java.util.List;

import ru.urfc.portal.exceptions.InternalException;

public class BaseUtil {

	public static class FoundExactlyOneException extends InternalException {
		public FoundExactlyOneException(String messageTemplate, Object... arguments) {
			super(messageTemplate, arguments);
		}
	}
	
	static public <T> T exactlyOne(List<T> list) {
		if (list.size() == 1) 
			return list.get(0);
		throw new FoundExactlyOneException("Found ''{0}'' items", list.size());
	}

	public static class FoundOneOrNullException extends InternalException {
		public FoundOneOrNullException(String messageTemplate, Object... arguments) {
			super(messageTemplate, arguments);
		}
	}
	
	static public <T> T oneOrNull(List<T> list) {
		if (list.size() == 0) {
			return null;
		} if (list.size() == 1) {
			return list.get(0);
		}
		throw new FoundOneOrNullException("Found ''{0}'' items", list.size());
	}

}
